<?php

/* 
 * 订单管理
 * @2016-01-10 wzl
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class orderController extends AppController
{
    public function __construct() {
        parent::__construct();
        $this->modelObj = new OrderInfoModel();
        $this->log_type = '订单管理';
        $this->cookie_key = 'order_cookie';
    }

    /**
     * 浏览
     */
    public function view(){
        $this->get_data();
        $this->display('order_list');
    }
    public function query(){
        $this->get_data();        
        $this->ret['status'] = 0;
        $this->ret['content'] = $this->fetch('order_list');
        exit(json_encode($this->ret));
    }
    public function del() {
        $id = intval($_POST['id']);
        if($id > 0) {
            $bool = OrderInfoModel::load()->update_del($id);
            if($bool) {
                echo $this->setReturn(0, 'ok');exit;
            }
        }
        echo $this->setReturn(100, 'error');
    }

    /**
     * 获取数据
     */
    private function get_data(){
        $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
        if($this->checkIsAdmin()) {
            $where = '';            
        } else {
            $where = ' and is_show = 1 and is_del = 0 and user_id = ' . $this->user_id;
        }
        $filter['user_name'] = isset($_POST['user_name']) ? $_POST['user_name'] : '';
        $filter['start_time'] = isset($_POST['start_time']) ? $_POST['start_time'] : '';
        $filter['end_time'] = isset($_POST['end_time']) ? $_POST['end_time'] : '';
        $filter['model'] = isset($_POST['model']) ? $_POST['model'] : '';
        $filter['is_show'] = isset($_POST['is_show']) ? intval($_POST['is_show']) : '';
        $filter['to_time'] = isset($_POST['to_time']) ? intval($_POST['to_time']) : '';
        if(!empty($filter['user_name'])) {
            $user_id = AdminUserModel::load()->get_user_id($filter['user_name']);
            $where .= ' and user_id = ' . $user_id;
        }
        if(!empty($filter['start_time'])) {
            $start_time = publicController::load()->strToTime($filter['start_time']);
            $where .= ' and in_time < ' . $start_time;
        }
        if(!empty($filter['end_time'])) {
            $end_time = publicController::load()->strToTime($filter['start_time']);
            $where .= ' and in_time > ' . $end_time;
        }
        //选择模式
        if(!empty($filter['model'])) {
            switch ($filter['model']) {
                case 1://显示隐藏
                    $where .= ' and is_show = 0';
                    break;
                case 2://删除
                    $where .= ' and is_del = 1';
                    break;
                case 3://所有
                    $where .= ' and is_show in (0,1) and is_del in (0,1)';
                    break;
                default:
                    break;
            }            
        }
        //读取隐藏的数据
        if($filter['is_show'] == 1) {
            $where = str_replace('and is_show = 1', '', $where);
            $where .= ' and is_show = 0';
        }
        //友好时间
        if($filter['to_time']) {
            $to_time = 0;
            switch ($filter['to_time']) {
                case 1:
                    $where .= ' and to_days(FROM_UNIXTIME(in_time)) = TO_DAYS(now())';
                    break;
                case 2:
                    $where .= ' and TO_DAYS(NOW()) - TO_DAYS(FROM_UNIXTIME(in_time)) <= 1';
                    break;
                case 3:
                    $where .= ' and DATE_SUB(CURDATE(), INTERVAL 3 DAY) <= date(FROM_UNIXTIME(in_time))';
                    break;
                case 4:
                    $where .= ' and DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(FROM_UNIXTIME(in_time))';
                    break;
                case 5:
                    $where .= ' and DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(FROM_UNIXTIME(in_time))';
                    break;
                case 6:
                    $where .= " and DATE_FORMAT( FROM_UNIXTIME(in_time), '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )";
                    break;
                case 7:
                    $where .= " and PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( FROM_UNIXTIME(in_time), '%Y%m' ) ) =1";
                    break;
                case 8:
                    $where .= " and PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( FROM_UNIXTIME(in_time), '%Y%m' ) ) =3";
                    break;
                case 9:
                    $where .= " and PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( FROM_UNIXTIME(in_time), '%Y%m' ) ) =6";
                    break;
                case 10:
                    $where .= " and YEAR(FROM_UNIXTIME(in_time))=YEAR(NOW())";
                    break;
                case 11:
                    $where .= " and year(FROM_UNIXTIME(in_time))=year(date_sub(now(),interval 1 year))";
                    break;
                default:
                    break;
            }
        }
        $sort_by = isset($_REQUEST['sort_by']) ? $_REQUEST['sort_by'] : $this->modelObj->keyFields;
        $sort_order = isset($_REQUEST['sort_order']) ? $_REQUEST['sort_order'] : 'DESC';
        $pageObj = new pageModel('order_info', $where, $this->page_number, 10, $sort_by, $sort_order);        
        $data = $pageObj->pageData($page);    
        $list = $data['pageData'];
        if($list) {
            foreach ($list as $key => $row) {
                $data['pageData'][$key]['id'] = $key + 1;
                
                $user_name = AdminUserModel::load()->get_user_name($row['user_id']);
                $data['pageData'][$key]['game_name'] = CodeModel::load()->get_game_name($row['code_id']);
                $data['pageData'][$key]['user_name'] = $user_name;
                $data['pageData'][$key]['in_time_format'] = publicController::load()->friendlyDate($row['in_time']);
                $data['pageData'][$key]['is_show'] = $row['is_show'] == 1 ? 'glyphicon-ok' : 'glyphicon-remove';
                $data['pageData'][$key]['is_del'] = $row['is_del'] == 1 ? 'glyphicon-ok' : 'glyphicon-remove';    
                $content = $row['content'];
                if($content) {
                    $data['pageData'][$key]['content'] = unserialize($content);
                }
            }
        }
        $this->assign('isadmin', $this->checkIsAdmin());
        $this->assign('sort_' . $sort_by, $data['sort_img']);
        $this->assign('data', $data);
    }    
}
